<template>
  <div class="carousel-container">
    <el-carousel 
      :interval="5000" 
      arrow="hover" 
      :height="carouselHeight + 'px'"
      :autoplay="true"
    >
      <el-carousel-item v-for="item in carouselItems" :key="item.id">
        <div class="carousel-item">
          <div class="carousel-image-wrapper">
            <el-image 
              :src="'api'+item.imageUrl" 
              fit="cover"
              class="carousel-image"
            >
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </div>
          <div class="carousel-content">
            <div class="content-wrapper">
              <span class="tag">{{ item.tag }}</span>
              <h2 class="title">{{ item.title }}</h2>
              <p class="description">{{ item.description }}</p>
              <div class="price-section" v-if="item.product && item.product.isDiscount">
                <span class="currency">¥</span>
                <span class="price">{{ item.product.discountPrice }}</span>
                <span class="original-price">
                  ¥{{ item.product.price }}
                </span>
              </div>

              <div class="price-section" v-else-if="item.product">
                <span class="currency">¥</span>
                <span class="price">{{ item.product.price }}</span>
              </div>
              <el-button 
                type="success" 
                round 
                class="action-button"
                @click="handleView(item)"
              >
                {{ item.product ? '立即购买' : '查看详情' }}
              </el-button>
            </div>
          </div>
        </div>
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<script>
import Request from '@/utils/request'

export default {
  name: 'FrontCarousel',
  data() {
    return {
      carouselItems: [],
      carouselHeight: 0
    }
  },
  mounted() {
    this.calculateHeight()
    window.addEventListener('resize', this.calculateHeight)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.calculateHeight)
  },
  created() {
    this.getCarouselItems()
  },
  methods: {
    calculateHeight() {
      const containerWidth = this.$el.clientWidth
      this.carouselHeight = Math.floor(containerWidth / 3)
    },
    async getCarouselItems() {
      try {
        const res = await Request.get('/carousel/active')
        if (res.code === '0') {
          this.carouselItems = res.data
        }
      } catch (error) {
        console.error('获取轮播图数据失败:', error)
      }
    },
    handleView(item) {
      if (item.product) {
        this.$router.push(`/product/${item.product.id}`)
      }
    }
  }
}
</script>

<style scoped>
.carousel-container {
  margin: 24px auto;
  max-width: 1400px;
  border-radius: 22px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(103,194,58,0.10);
  width: 100%;
  background: linear-gradient(135deg, #e8f5e9 0%, #fffde7 100%);
  position: relative;
}

.carousel-item {
  height: 100%;
  display: flex;
  position: relative;
}

.carousel-image-wrapper {
  width: 100%;
  height: 100%;
  position: relative;
}

.carousel-image {
  width: 100%;
  height: 100%;
  transition: transform 0.6s ease;
}

.carousel-content {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  background: linear-gradient(90deg, rgba(103,194,58,0.18) 0%, rgba(0,0,0,0.4) 50%, rgba(0,0,0,0) 100%);
}

.content-wrapper {
  padding: 0 80px;
  max-width: 600px;
  color: white;
  position: relative;
}

.content-wrapper::before {
  content: '';
  position: absolute;
  left: -60px;
  top: 30px;
  width: 60px;
  height: 60px;
  background: url('data:image/svg+xml;utf8,<svg width="60" height="60" viewBox="0 0 60 60" fill="none" xmlns="http://www.w3.org/2000/svg"><ellipse cx="30" cy="30" rx="28" ry="18" fill="%23e8f5e9"/><path d="M15 40 Q30 10 45 40" stroke="%2367c23a" stroke-width="3" fill="none"/><ellipse cx="30" cy="30" rx="6" ry="12" fill="%23a5d6a7"/></svg>') no-repeat center/contain;
  opacity: 0.7;
}

.tag {
  display: inline-block;
  padding: 6px 16px;
  background: linear-gradient(90deg, #67c23a 60%, #a5d6a7 100%);
  color: white;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 20px;
  letter-spacing: 1px;
}

.title {
  font-size: 44px;
  font-weight: 700;
  margin: 0 0 20px;
  line-height: 1.2;
  letter-spacing: 1.5px;
}

.description {
  font-size: 18px;
  line-height: 1.6;
  margin: 0 0 30px;
  opacity: 0.9;
}

.price-section {
  margin-bottom: 30px;
}

.currency {
  font-size: 20px;
  font-weight: 500;
  vertical-align: baseline;
}

.price {
  color: #43a047;
}

.original-price {
  font-size: 16px;
  color: #909399;
  text-decoration: line-through;
  margin-left: 8px;
}

.action-button {
  padding: 14px 36px;
  font-size: 18px;
  font-weight: 600;
  background: linear-gradient(90deg, #67c23a 60%, #43a047 100%);
  border-color: #67c23a;
  border-radius: 24px;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px #a5d6a733;
}

.action-button:hover {
  background: linear-gradient(90deg, #43a047 0%, #67c23a 100%);
  transform: translateY(-2px) scale(1.03);
  box-shadow: 0 4px 16px rgba(103, 194, 58, 0.18);
}

.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 30px;
}

/* 修改轮播图指示器样式 */
:deep(.el-carousel__indicators) {
  bottom: 30px; /* 调整指示器位置 */
}

:deep(.el-carousel__indicator) {
  padding: 12px 4px;
}

:deep(.el-carousel__button) {
  width: 30px;
  height: 3px;
  background-color: rgba(255, 255, 255, 0.4); /* 降低默认状态的不透明度 */
  border-radius: 3px;
  transition: all 0.3s ease;
}

:deep(.el-carousel__indicator.is-active .el-carousel__button) {
  background-color: #fff; /* 激活状态使用纯白色 */
}

/* 修改箭头样式 */
:deep(.el-carousel__arrow) {
  background-color: rgba(0, 0, 0, 0.3);
  border-radius: 50%;
  width: 44px;
  height: 44px;
  transition: all 0.3s ease;
}

:deep(.el-carousel__arrow:hover) {
  background-color: rgba(0, 0, 0, 0.6);
}

/* 移除 indicator-position="outside" 相关样式 */
.el-carousel {
  margin-bottom: 0; /* 移除底部空白 */
}

:deep(.el-carousel__container) {
  margin-bottom: 0; /* 确保容器底部没有空白 */
}

@media (max-width: 900px) {
  .content-wrapper {
    padding: 0 18px;
    max-width: 90vw;
  }
  .title {
    font-size: 28px;
  }
  .carousel-container {
    border-radius: 12px;
  }
}
</style> 